#ifndef _SERVER_UTILS_H_
#define _SERVER_UTILS_H_

#include "util.h"
#include "bid_service.h"
#include "prov_service.h"
#include "admin_service.h"


/* datastructures */
typedef struct SERVER_CONTEXT_DEF_ {
	//Config
    char* server_host;     //ip or hostname
    int bid_port;       // server port number
    int admin_port;        // admin service port number
    int prov_port;         // provisioning service port number
    int mode;              //operation mode.

    boolean graceful_shutdown_request;

    int bid_fd;
    int prov_fd;
    int admin_fd;


    //Statistics
    time_t server_start_time;
    long curr_sessions_count;     //active number of sessions.
    long active_sessions_watermark;
    long avg_session_duration;
    long max_session_duration;
    long min_session_duration;
    long total_sessions_served;
}SERVER_GLOBALS;

extern SERVER_GLOBALS g;


typedef void sigfunc(int);

typedef enum SERVER_ERROR_DEF {
    SRVRE_SUCCESS,
    SRVRE_CRITICAL_ERROR,
    SRVRE_GRACEFUL_SHUTDOWN,
    SRVRE_INTERRUPT,
}SERVER_ERROR;

/* routines */
void shutdown_server(int error, char *errmsg);
void request_graceful_shutdown();
void request_graceful_shutdown();

sigfunc *set_signal_handler(int signo, sigfunc *func);

//signal handlers
void sig_chld(int signo);
void sig_alrm(int signo);
void sig_int(int signo);
void sig_pipe(int signo);


#endif //_SERVER_UTILS_H_
